package com.gainsoft.grevocabuilder;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class Word
{
    public static final String DATABASE_NAME = "word";
    public static final String WORD_TABLE = "word";
    public static final String ID_COL = "_id";
    public static final String WORD_COL = "word";
    public static final String MEANING_COl = "meaning1";
    public static final String LERANINGINDEX_COL = "learningIndex";
    public static final int DATABASE_VERSION = 1;
    public static final String DB_PATH = "/data/data/com.gainsoft.grevocabuilder/databases/";

    private int id_;
    private static SQLiteDatabaseAdapter mSQLiteDatabaseAdapter;
    private static boolean getFirstRun = true;
    private static SQLiteDatabase db;
    public static Context mContext;

    static private SQLiteDatabase initializeDatabase()
    {
	mSQLiteDatabaseAdapter = SQLiteDatabaseAdapter.getInstance(mContext,
		DATABASE_NAME);
	if (!SQLiteDatabaseAdapter.checkDatabase(mContext, DATABASE_NAME))
	{
	    db = mSQLiteDatabaseAdapter.getDatabase();

	} else
	{
	    db = mSQLiteDatabaseAdapter.getReadableDatabase();

	}
	return db;
    }

    public int getLearningIndex_()
    {
	return learningIndex_;
    }

    public void setId_(int id_)
    {
	this.id_ = id_;
    }

    public void setWord_(String word_)
    {
	this.word_ = word_;
    }

    public void setMeaning_(String meaning_)
    {
	this.meaning_ = meaning_;
    }

    public void setLearningIndex_(int learningIndex_)
    {
	this.learningIndex_ = learningIndex_;
    }

    private String word_;
    private String meaning_;
    private int learningIndex_ = 0;

    private Word(int id, String word, String meaning, int learningIndex)
    {
	super();
	this.id_ = id;
	this.word_ = word;
	this.meaning_ = meaning;
	this.learningIndex_ = learningIndex;
    }

    public Word()
    {
	// TODO Auto-generated constructor stub
    }

    public static Word getWordById(int id)
    {
	Word word = new Word();
	String[] args =
	{ Integer.toString(id) };
	initializeDatabase();
	Cursor c;
	try
	{

	    c = db.rawQuery("SELECT * FROM  " + WORD_TABLE + "  " + " WHERE  "
		    + ID_COL + " = ?", args);
	    c.moveToFirst();

	    word.setId_(c.getInt(c.getColumnIndex(ID_COL)));
	    word.setWord_(c.getString(c.getColumnIndex(WORD_COL)));
	    word.setMeaning_(c.getString(c.getColumnIndex(MEANING_COl)));
	    word.setLearningIndex_(c.getInt(c.getColumnIndex(LERANINGINDEX_COL)));
	} catch (Exception e)
	{
	    Log.e("test", e.toString());
	}
	return word;
    }

    public static Cursor getWordCursorByAlphabet(String alphabets)
    {
	Cursor c = null;

	String[] args =
	{ alphabets };
	initializeDatabase();
	try
	{

	    c = db.rawQuery("SELECT * FROM  " + WORD_TABLE + "  " + " WHERE  "
		    + WORD_COL + " Like '" + alphabets + "%' order by "
		    + LERANINGINDEX_COL, null);

	} catch (Exception e)
	{
	    Log.e("test", e.toString());
	}
	return c;
    }

    public int getId_()
    {
	return id_;
    }

    public String getWord_()
    {
	return word_;
    }

    public String getMeaning_()
    {
	return meaning_;
    }

    public int getLearningIndex()
    {
	return learningIndex_;
    }

    public void setLearningIndex(int learningIndex)
    {
	this.learningIndex_ = learningIndex;
    }

}
